﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToPLMX.ViewModels.CUiETPXOutputViewModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToPLMX.Commands;
using System;
using System.IO;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToPLMX.ViewModels;

public class CUiETPXOutputViewModel : ObservableObject
{
  private string _exportPath;
  private bool _isValidExportPath;
  private bool? _launchOnCompletion;
  private string _packageName;
  private readonly string EXPORT_PATH_KEY = nameof (ExportPath);
  private readonly string LAUNCH_ON_COMPLETION_KEY = nameof (LaunchOnCompletion);
  public const string EXPORT_EXTENSION = ".plmx";

  public string ExportPath
  {
    get => this._exportPath;
    set => this.Set<string>(ref this._exportPath, value, nameof (ExportPath));
  }

  public bool IsValidExportPath
  {
    get
    {
      return Directory.Exists(Path.GetDirectoryName(this.ExportPath)) && Path.GetExtension(this.ExportPath).Equals(".plmx");
    }
    set => this.Set<bool>(ref this._isValidExportPath, value, nameof (IsValidExportPath));
  }

  public bool? LaunchOnCompletion
  {
    get => this._launchOnCompletion;
    set => this.Set<bool?>(ref this._launchOnCompletion, value, nameof (LaunchOnCompletion));
  }

  public string PackageName
  {
    get => this._packageName;
    set => this.Set<string>(ref this._packageName, value, nameof (PackageName));
  }

  public CUiETPXBrowseCommand BrowseCommand { get; set; }

  public CUiETPXOutputViewModel()
  {
    this.BrowseCommand = new CUiETPXBrowseCommand(new Action(this.BrowseFiles));
    this.LoadData();
  }

  public void PersistData()
  {
    CCoExportToPLMXUtils.SaveValueToRegistry(this.EXPORT_PATH_KEY, Path.GetDirectoryName(this.ExportPath));
    CCoExportToPLMXUtils.SaveValueToRegistry(this.LAUNCH_ON_COMPLETION_KEY, this.LaunchOnCompletion.ToString());
  }

  private void LoadData()
  {
    string name = ((ITxObject) TxApplication.ActiveDocument.CurrentStudy).Name;
    this.ExportPath = CCoExportToPLMXUtils.ReadValueFromRegistry(this.EXPORT_PATH_KEY);
    if (!string.IsNullOrEmpty(this.ExportPath))
      this.ExportPath = $"{this.ExportPath}\\{name}.plmx";
    this.PackageName = name;
    string str = CCoExportToPLMXUtils.ReadValueFromRegistry(this.LAUNCH_ON_COMPLETION_KEY);
    this.LaunchOnCompletion = new bool?(string.IsNullOrEmpty(str) || str.Equals("True"));
  }

  private void BrowseFiles()
  {
    SaveFileDialog saveFileDialog = this.InitializeDialog();
    if (saveFileDialog.ShowDialog() != DialogResult.OK)
      return;
    this.ExportPath = saveFileDialog.FileName.EndsWith(".plmx") ? saveFileDialog.FileName : saveFileDialog.FileName + ".plmx";
  }

  private SaveFileDialog InitializeDialog()
  {
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.DefaultExt = ".plmx";
    saveFileDialog.FileName = ((ITxObject) TxApplication.ActiveDocument.CurrentStudy).Name;
    saveFileDialog.Filter = "plmx files (*.plmx)|*.plmx|All files (*.*)|*.*";
    saveFileDialog.RestoreDirectory = true;
    saveFileDialog.InitialDirectory = string.IsNullOrEmpty(this.ExportPath) || !Directory.Exists(Path.GetDirectoryName(this.ExportPath)) ? "C:\\" : Path.GetDirectoryName(this.ExportPath);
    saveFileDialog.OverwritePrompt = false;
    saveFileDialog.Title = CCoExportToPLMXUtils.GetResourceManager().GetString("ETPX_BROWSE_DIALOG_TITLE");
    return saveFileDialog;
  }
}
